package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 修改套餐
     * @param setmealDTO
     */
    @Transactional
    public void insert(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        Long id = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(id);
            return item;
        }).collect(Collectors.toList());
        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 分页查询
     *
     * @param setmealPage
     * @return
     */
    public PageResult list(SetmealPageQueryDTO setmealPage) {
        PageHelper.startPage(setmealPage.getPage(),setmealPage.getPageSize());

        List<SetmealVO> setmealVOList = setmealMapper.list(setmealPage);
        setmealVOList = setmealVOList.stream().map((item)->{
            Long categoryId = item.getCategoryId();
            String name = categoryMapper.getById(categoryId);
            item.setCategoryName(name);
            return item;
        }).collect(Collectors.toList());

        PageInfo<SetmealVO> pageInfo = new PageInfo<>(setmealVOList);

        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    /**
     * 批量删除套餐
     * @param ids
     */
    @Transactional
    public void del(List<Long> ids) {
        setmealDishMapper.del(ids);
        setmealMapper.del(ids);
    }

    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    public SetmealVO getById(Long id) {
        // 获取指定id的setmeal所有信息
        SetmealVO setmealVO = setmealMapper.getById(id);
        // 根据套餐的菜品ID查询菜品名称
        String byId = categoryMapper.getById(setmealVO.getCategoryId());
        setmealVO.setCategoryName(byId);

        // 获取套餐的菜品
        List<SetmealDish> setmealDish = setmealDishMapper.getById(setmealVO.getId());
        setmealVO.setSetmealDishes(setmealDish);

        return setmealVO;
    }

    /**
     * 套餐起售、停售
     * @param status
     * @param id
     */
    public void startOrStop(Setmeal setmeal) {
        setmealMapper.update(setmeal);
    }

    /**
     * 修改套餐
     * @param setmealVO
     */
    public void update(SetmealVO setmealVO) {
        // 先将套餐信息（setmeal）存入
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealVO,setmeal);
        setmealMapper.update(setmeal);

        setmealDishMapper.del(Collections.singletonList(setmealVO.getId()));

        // 在将套餐和菜品关联表（setmeal_dish）存入
        List<SetmealDish> setmealDishes = setmealVO.getSetmealDishes().stream().map((item)->{
            item.setSetmealId(setmeal.getId());

            return item;
        }).collect(Collectors.toList());

        setmealDishMapper.insert(setmealDishes);
    }

    /**
     * 根据分类id查询套餐
     * @param setmeal
     * @return
     */
    public List<Setmeal> listWithStatus(Setmeal setmeal) {
        List<Setmeal> setmealList = setmealMapper.listWithStatus(setmeal);

        return setmealList;
    }

    /**
     * 根据套餐id查询包含的菜品
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealDishMapper.getDishItemBySetmealId(id);
    }


}
